package com.proxy.demo01;

public class Proxy implements Searcher {
    private RealSearcher searcher;
    private AccessValidator accessValidator;
    private UsageLogger usageLogger;

    public Proxy(){
        searcher = new RealSearcher();
    }

    private boolean checkAccess(String userId){
        accessValidator = new AccessValidator();
        return accessValidator.validateUser(userId);
    }

    private void logUsage(String userId){
        usageLogger = new UsageLogger();
        usageLogger.save(userId);
    }

    @Override
    public String doSearch(String userId, String searchType) {
        if (checkAccess(userId)) {
            String result = searcher.doSearch(userId, searchType);
            logUsage(userId);
            return result;
        }
        return null;
    }
}
